package dawnland03.data.map.io.reader.sax;

import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import dawnland03.data.map.model.Matrix;

/**
 * User: Petru Obreja (obrejap@yahoo.com)
 * Date: Sep 16, 2009
 * Time: 8:55:36 PM
 */
public class MatrixXMLSAXParser {
    private MatrixXMLSAXHandler handler;
    private SAXParser saxParser;


    public MatrixXMLSAXParser(MatrixXMLSAXHandler handler) {
        this.handler = handler;
        create();
    }

    private void create() {
        try {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            factory.setNamespaceAware(true);
            factory.setValidating(true);
            saxParser = factory.newSAXParser();
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

    public Matrix parse(String uri) {
        try {
            saxParser.parse(uri, handler);
            return handler.getMatrix();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
